package com.one.group.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.admin.service.impl.SpPredepositSerciveImpl;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.PayerBillQueryDto;
import com.one.group.model.dto.PayerlistDto;
import com.one.group.model.dto.SpPredepositAddDto;
import com.one.group.model.entity.SpPredeposit;
import com.one.group.model.vo.SpPredepositVo;
import com.one.group.result.Result;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/SpPredepositController")
@Tag(name = "预存款相关接口")
public class SpPredepositController {
    @Autowired
    private SpPredepositSerciveImpl spPredepositService;
    @PostMapping("/querybypayer")
    @Operation(summary = "通过预存人查询预存款")
    public Result querybypayer(@RequestBody PayerBillQueryDto payerBillQueryDto) {
        return ResultUtil.success(spPredepositService.queryByPayer(payerBillQueryDto));
    }
    @PostMapping("/add")
    @Operation(summary = "添加预存款")
    public Result add(@RequestBody SpPredepositAddDto spPredepositAddDto) {
        if(spPredepositService.predepositAdd(spPredepositAddDto))
            return ResultUtil.success();
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PutMapping("/update")
    @Operation(summary = "修改预存款")
    public Result update(@RequestBody SpPredepositAddDto spPredepositAddDto) {
        if(spPredepositService.predepositUpdate(spPredepositAddDto))
            return ResultUtil.success();
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除预存款")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto) {
        return ResultUtil.success(spPredepositService.removeByIds(baseDeleteDto.getIds()));
    }
    @PostMapping("/payerlist")
    @Operation(summary = "预存人列表")
    public Result payerlist(@RequestBody PayerlistDto payerlistDto) {
        PageHelper.startPage(payerlistDto.getPage(),payerlistDto.getLimit());
        List<SpPredepositVo> payerlist = spPredepositService.payerlist(payerlistDto);
        PageInfo<SpPredepositVo> pageInfo = new PageInfo<>(payerlist);
        return ResultUtil.success(pageInfo);
    }
    @GetMapping("/totalamount")
    @Operation(summary = "预存总金额")
    public Result totalamount() {
        return ResultUtil.success(spPredepositService.totalamount());
    }
}
